package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Teacher;
import service.TeacherService;



/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TeacherService teacherService=new TeacherService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("update".contentEquals(method)) {
			update(request,response);
		}else if("save".contentEquals(method)) {
			save(request,response);
		}else if("add".contentEquals(method)) {
			add(request,response);
		}else if("delete".contentEquals(method)) {
			delete(request,response);
		}
	
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id=Integer.valueOf(idStr);
		teacherService.delete(id);
		response.sendRedirect("teacher");
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("jsp/teacher/edit.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		Integer id=null;
		if(idStr!=null&&!"".contentEquals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		String name = request.getParameter("name");
		String password=request.getParameter("password");
		String username = request.getParameter("username");
		Teacher Teacher=new Teacher(id, name, password,username);
		teacherService.save(Teacher);
		response.sendRedirect("teacher");
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String idStr = request.getParameter("id");
		Integer id=Integer.parseInt(idStr);
		Teacher model = teacherService.findById(id);
		request.setAttribute("model",model);
		request.getRequestDispatcher("jsp/teacher/edit.jsp").forward(request, response);
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Teacher> teacher=teacherService.findAll();
		request.setAttribute("list", teacher);
		request.getRequestDispatcher("jsp/teacher/list.jsp").forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
